//
//  LoadShowEditeImageView.m
//  WULIHH
//
//  Created by tang on 16/5/3.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "LoadShowEditeImageView.h"
#import "Header.h"
#import "IQStickerView.h"

@interface LoadShowEditeImageView ()<IQStickerViewDelegate>
{
    IQStickerView *qipaoView;
    NSMutableDictionary *mutableDic;
    NSString *imageString;
    
//    NSMutableDictionary *ChangeInfoDic;
}

@end


@implementation LoadShowEditeImageView



- (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
    //删除字符串中的空格
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6)
    {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0X"])
    {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6)
    {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
//        ChangeInfoDic = [NSMutableDictionary new];
        [self setUserInteractionEnabled:YES];
        [self setClipsToBounds:YES];
    }
    return self;
}



- (void)setImage:(UIImage *)image
{
    [super setImage:image];
    
    float imageWith = image.size.width/2.0;
    float imageHeight = image.size.height/2.0;
    float MAXHeight = heightView-106.0;
    
    
//        NSLog(@"++++++++%f   %f", imageWith, imageHeight);
    if (image.size.width/2.0 <= WithView && image.size.height/2.0 <= MAXHeight)
    {
        [_MLoadShowEditeImageViewDelegate withImageFrame:CGSizeMake(imageWith, imageHeight)];
//        NSLog(@"%f    %f",  image.size.width, imageHeight);
    }
    else
    {
        if (image.size.width > image.size.height)   //宽图
        {
            float PoitK = imageWith/imageHeight;
            [_MLoadShowEditeImageViewDelegate withImageFrame:CGSizeMake(WithView, WithView/PoitK)];
        }
        else
        {
            float PoitK = imageWith/imageHeight;
            [_MLoadShowEditeImageViewDelegate withImageFrame:CGSizeMake(PoitK*MAXHeight, MAXHeight)];
            
        }
    }

}



- (void)loadImage:(UIImage *)image
{
//    NSLog(@"%f", image.size.width);
    
//    float k = image.size.width/image.size.height;
}



//加载气泡
- (void)loadTextQipao:(NSDictionary *)dic
{

    float w = 0.0;
    float h = 0.0;
    [qipaoView removeFromSuperview];
    
    mutableDic = [[NSMutableDictionary alloc] initWithDictionary:dic];
    
    
    if (dic == nil)
    {
        //不加载气泡
    }
    else
    {
        imageString = [dic valueForKey:@"image"];
        w = [[dic valueForKey:@"width"] floatValue];
        h = [[dic valueForKey:@"height"] floatValue];
        
        
        qipaoView = [[IQStickerView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
        [qipaoView setDelegate:self];
        [qipaoView setImage:[UIImage imageNamed:imageString]];
        [qipaoView setCenter:CGPointMake([[dic valueForKey:@"centerX"] intValue], [[dic valueForKey:@"centerY"] intValue])];
        [qipaoView setJiaoduFloat:[[dic valueForKey:@"jiaodu"] floatValue]];
        
        
        CGFloat x = [[dic valueForKey:@"contX"] floatValue];
        CGFloat y = [[dic valueForKey:@"contY"] floatValue];
        
        qipaoView.rect = CGRectMake(x, y, [[dic valueForKey:@"lablewidth"] floatValue], [[dic valueForKey:@"lableheight"] floatValue]);
        [qipaoView setTextString:[dic valueForKey:@"twxt"]];
        [qipaoView setTextColor:[self colorWithHexString:[dic valueForKey:@"color"] alpha:1.0]];
        [self addSubview:qipaoView];
        
        [qipaoView setImageAlpha:[[dic valueForKey:@"alpha"] floatValue]];
        [qipaoView showBiankuang];
    }

    
}




//裁剪图片
- (UIImage *)cropImage
{
    [qipaoView hideBiangkuang];
    UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, [UIScreen mainScreen].scale);
    
    [self drawViewHierarchyInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) afterScreenUpdates:YES];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [qipaoView showBiankuang];
    return image;
}




- (void)saveQipaoBounds:(CGRect)rect withJiaodu:(CGFloat)jiaodu withLableFrameX:(float)lableX withY:(float)lableY withLableW:(float)lablewidth withlableH:(float)lableheight withContent:(NSString *)string withAlpha:(float)alpha withTextColor:(NSString *)TextColor
{
    
    [mutableDic setDictionary:@{@"image":imageString,
                                   @"width":[NSString stringWithFormat:@"%f", qipaoView.bounds.size.width],
                                   @"height":[NSString stringWithFormat:@"%f", qipaoView.bounds.size.height],
                                   @"jiaodu":[NSString stringWithFormat:@"%f", jiaodu],
                                   @"contX":[NSString stringWithFormat:@"%f", lableX],
                                   @"contY":[NSString stringWithFormat:@"%f", lableY],
                                   @"lablewidth":[NSString stringWithFormat:@"%f", lablewidth],
                                   @"lableheight":[NSString stringWithFormat:@"%f", lableheight],
                                   @"twxt":string,
                                   @"alpha":[NSString stringWithFormat:@"%f", alpha],
                                   @"color":TextColor,
                                   @"centerX":[NSString stringWithFormat:@"%f", qipaoView.center.x],
                                   @"centerY":[NSString stringWithFormat:@"%f", qipaoView.center.y]}];
    
    [_MLoadShowEditeImageViewDelegate setQipaoDic:mutableDic];
}


- (void)setContentString:(NSString *)contentString
{
    [mutableDic setValue:contentString forKey:@"twxt"];
    [qipaoView setTextString:contentString];
}

//更改透明度
- (void)setAlphaValue:(float)alphaValue
{
    [mutableDic setValue:[NSString stringWithFormat:@"%f", alphaValue] forKey:@"alpha"];
    [_MLoadShowEditeImageViewDelegate setQipaoDic:mutableDic];
    [qipaoView setImageAlpha:alphaValue];
}

//改变文字颜色
- (void)setChangeTextColor:(NSString *)changeTextColor
{
    [mutableDic setValue:changeTextColor forKey:@"color"];
    [qipaoView setTextColor:[self colorWithHexString:changeTextColor alpha:1.0]];
    [_MLoadShowEditeImageViewDelegate setQipaoDic:mutableDic];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
